<?php 

class AdminNewsPresenter extends AdminPresenter{
	
	private $model;
	/**
	 * Start lifecycle presenter
	 *
	 * @return void
	 */
	protected function startup(){
		parent::startup();
		$this->template->jsFile[] = "inc/news.js";
		$this->model = NewsModel::getInstance();
	}

	/**
	 * Settings for all render
	 *
	 * @return void
	 */
	protected function beforeRender(){

	}

	/**
	 * Render default action
	 *
	 * @return void
	 */
	public function renderDefault($id){
		
		$this->template->title = "Přehled novinek";
		$this->template->id = $id;
	}
	
	protected function createComponentGrid($name){
		
		$grid = new DataGrid;
	    $grid->bindDataTable($this->model->getDataSource(array("id_language" => $this->session->admin_id_language)));
	    
	    $grid->addColumn("title", "Nadpis")->addFilter();
	    $grid->addDateColumn('date', 'Datum', "%d.%m.%Y");
	    //$grid->addColumn("content", "Obsah", 50)->addFilter();
	    $grid->addColumn("perex", "Perex")->addFilter();
	    
	    // nastavíme klíč pro akce (a také i pro operace, o těch později)
		$grid->keyName = 'id_news';
		
		// přidáme sloupec pro akce (sloupců může být i více)
		$grid->addActionColumn('Actions');
		
		// a naplníme datagrid akcemi pomocí továrničky
		$grid->addAction('Nový', 'addNews', Html::el('span')->class('icon icon-edit'), $useAjax = FALSE);
		$grid->addAction('Smaž', 'deleteNews', Html::el('span')->class('icon icon-del'), $useAjax = TRUE);
			    
	    $this->addComponent($grid, "grid");
	}
	
	public function actionAddNews($id){

		$this->template->title = "Přidat Novinku";
		$this->template->edit = $this->model->getNewById($_GET["id_news"]);
		$this->template->picture = $this->model->getPicture($this->template->edit["id_file"]);
		$this->template->id_lang = $this->session->admin_id_language;
		$this->template->id = $id;
	}
	
	
	//TODO redirecty vyresit
	public function actionSaveNew(){
		
		$values = $_POST;
		$values['date'] = new DateTime();
		
		$values['seo_title'] = $this->model->createSeo($values["title"]);
		
		$item = $this->system->getMenuItemByname($this->getRequest()->params["id"]);
		$values['id_menu'] = $item[0]["id_menu"];
		
		// odesleme newsletter
		if($values["newsletter"] == "ano"){
			
			$emails = $this->model->getEmails();
			// nyni je reseno: pro kazdou adresu novy email, kvuli generovani odhlasovaci adresy
			foreach($emails as $e){
				try {
					$mail = new Mail();
					$mail->setFrom("newsletter@pronax.cz");
					$mail->addTo($e["email"]);
					//$mail->addTo("t.voslar@seznam.cz");
					$mail->setSubject("Newsletter");
					// telo emailu
					
					$mailBody = new Template();
					$mailBody->registerFilter(new LatteFilter);
					
					$mailBody->emailData = $_POST;
					$mailBody->email = $e["email"];
					$hash = sha1($e["email"] . 'salt');
					$mailBody->link = $this->link("News:", array("id"=>$id, "seo_title"=>$n["odhlasit"])) . '/?hash=' . $hash;
					$mailBody->setFile(APP_DIR."/templates/News/newsletter.phtml");
					
					$mail->setHTMLBody($mailBody);
					// antispam
					$mail->send();
				} catch (Exception $er) {
					
					$this->flashMessage("Nepodařilo se odeslat email na adresu " . $e["email"] . ".", "error");
				}
				
			}
			
		}
		unset($values["newsletter"]);
		
		$this->model->saveNews($values);
		
		
		$this->flashMessage("Novinka byla uložena.", "ok");
		$this->redirect("AdminNews:default", $this->getParam("id"));
	}
	
	public function actionDeleteNews($id_news){
		
		$this->model->deleteNews($id_news);
		$this->flashMessage("Novinka byla smazána.", 'ok');
		$this->redirect("AdminNews:default", $this->getParam("id"));
	}
}

?>